#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021-12-13 14:38
# @Author : huabao.tan
# @File : demo5.py
# @Software: PyCharm

import requests
from time import sleep
import re


class Daily:

    def __init__(self):
        self.response = Daily.get_request(self)
        self.data = Daily.data(self)
        self.id = Daily.id(self)

    def url(self):
        return "https://www.jx3api.com/app/daily"

    def params(self):
        return '{"server":"长安城"}'

    def get_request(self):
        res = requests.get(url=Daily.url(self), params=Daily.params(self))
        return res.json()

    def id(self):
        res = self.response
        res = res['data']['dayWar']
        return res

    def data(self):
        res = self.response
        res = res['data']['date']
        return res

    def week(self):
        res = self.response
        res = res['data']['week']
        return res

    def week_team(self):
        res = self.response
        res = res['data']['weekTeam']
        res = str(res).split(';')
        result = ""
        for tempStr in res:
            result += "[ " + tempStr + " ]"

        return result

    def daily(self):
        print('时间：%s' % self.data + '\n' + str('星期：星期%s' % Daily.week(self)) + '\n' + '秘境日常：%s' % self.id
              + '\n' + '周常副本：%s' % Daily.week_team(self))


class Discourse:

    def __init__(self):
        pass

    def get_request(self):
        url = 'https://www.jx3api.com/app/random'
        res = requests.get(url)
        return res.json()

    def discourse(self):
        res = Discourse.get_request(self)
        res = res['data']['text']
        return res


class Price:

    def __init__(self, name):
        self.name = name
        self.goods_json = Price.get_requset(self)

    def url(self):
        return 'https://www.jx3api.com/app/price?name=%s' % self.name

    def get_requset(self):
        try:
            response = requests.get(url=Price.url(self))
            return response.json()
        except Exception as e:
            return '输入的关键词错误'

    def goods_name(self):
        try:
            acc = self.goods_json
            return acc['data']['name']
        except Exception as e:
            return '输入的关键词错误'

    def goods_info(self):
        try:
            acc = self.goods_json
            acc = acc['data']['info']
            return acc
        except Exception as e:
            return '输入的关键词错误'

    def goods_picture(self):
        try:
            acc = self.goods_json
            acc = acc['data']['upload']
            return acc
        except Exception as e:
            return '输入的关键词错误'

    def goods_price(self):
        try:
            acc = self.goods_json
            acc = acc['data']['data']
            for a in acc:
                for b in a:
                    res = b['price']
                    return res
        except Exception as e:
            return '输入的关键词错误'

    def late_sale_time(self):
        try:
            acc = self.goods_json
            acc = acc['data']['data']
            for a in acc:
                for b in a:
                    res = b['time']
                    return res
        except Exception as e:
            return '输入的关键词错误'


def query_info():
    inputs = input('输入要查询的物品信息：')
    print('--' * 20)
    p = Price('%s' % inputs)
    print(
        '名字：%s' % p.goods_name() + '\n' + '物品信息：%s' % p.goods_info() + '\n' + '最近交易价格：%s' % p.goods_price() + '\n' + '最近交易时间：%s' % p.late_sale_time() + '\n' + '物品图片地址：%s' % p.goods_picture())


def daily():
    print('--' * 30)
    d = Daily()
    d.daily()
    print('--' * 30)


def dis():
    print('--' * 30)
    d = Discourse()
    print(d.discourse())


class Announce:

    def url(self):
        return 'https://www.jx3api.com/app/announce'

    def params(self):
        return '{"limit": 5}'

    def res(self):
        return requests.get(url=self.url(), params=self.params()).json()

    def nwe_announce(self):
        res = self.res()['data']
        for a in res:
            title = a['title']
            date = a['date']
            url = a['url']
            return '标题：%s' % title, '时间：%s' % date, '相关链接：%s' % url

    def reg(self):
        result = str(self.nwe_announce())
        reg = "[(),'']"
        result = re.sub(reg, '', result)
        result = result.replace(' ', '\n')
        return result

class Serendipity:

    def __init__(self, server, name):
        self.server = server
        self.name = name

    def url(self):
        return 'https://www.jx3api.com/app/serendipity'

    def params(self):
        return {"server": "%s"%self.server,"name": "%s"%self.name, "serendipity": ""}

    def res(self):
        return requests.get(url=self.url(), params=self.params()).json()

if __name__ == '__main__':
    # a = Announce()
    # print(a.reg())
    # daily()
    # dis()
    # query_info()
    a = Announce()
    print(a.reg())